// pages/myqrcode/myqrcode.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cardno: null,
    send: true,
    alreadySend: false,
    second: 60,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      second: 60,
      alreadySend: true,
      send: false
    })
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  timer: function () {
    let promise = new Promise((resolve, reject) => {
      let setTimer = setInterval(
        () => {
          this.setData({
            second: this.data.second - 1
          })
          if (this.data.second <= 0) {
            this.setData({
              second: 60,
              alreadySend: false,
              send: true
            })
            resolve(setTimer)
          }
        }, 1000)
    })
    promise.then((setTimer) => {
      clearInterval(setTimer)
    })
  },

  btnRefresh: function () {
    //此处省略发送短信验证码功能
    // this.setData({
    //   alreadySend: true,
    //   send: false
    // })
    // this.timer()
    this.getData();
  },

  getData: function (e) {
    var that = this;
    var data = {
      "userid": app.globalData.userId,
      "kidid": app.globalData.kidId,
      "schoolid": app.globalData.schoolId,
      "appType": "1",
      "appKey": app.globalData.appKey,
      "timeSpan": app.globalData.timespan
    }
    wx.request({
      url: app.globalData.serverUrl,
      data: {
        "data": JSON.stringify(data),
        'act': "getQrCode"
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);
        if (res.data.status == 0) {
          that.setData({
            second: res.data.data.effect,
            alreadySend: true,
            send: false,
            cardno: res.data.data.url
          })
          that.timer();
        } else {
          wx.showToast({
            title: "获取信息失败",
            icon: 'none',
            duration: 1000,
            mask: true
          })
        }

      },
      fail: function (res) {
        wx.showToast({
          title: "获取信息失败",
          icon: 'none',
          duration: 1000,
          mask: true
        })
      },
      complete: function (res) {
        console.log('submit complete');
      }
    })
  }
})